//
//  CarouselView.m
//  美食百科
//
//  Created by dllo on 16/1/23.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "CarouselView.h"
#import "CarouselModel.h"
#import "UIImageView+WebCache.h"
@interface CarouselView()<UIScrollViewDelegate>
@property(nonatomic,retain)UIScrollView *carouselView;
@property(nonatomic, retain) NSTimer *timer;
@property(nonatomic, retain) NSArray *carouselArray;
@property(nonatomic,retain)CarouselModel *carouselModel;

/**
 *  轮播图是否被拖拽
 */
@property(nonatomic, assign) BOOL isDragging;
/**
 *  轮播图的页数控制
 */
@property(nonatomic, retain) UIPageControl *page;
@end
@implementation CarouselView
- (void)dealloc
{
    [_carouselView release];
    [_timer release];
    [_carouselArray release];
    [_page release];
    [_carouselModel release];
    [super dealloc];
}
-(id)initWithFrame:(CGRect)frame array:(NSMutableArray *)array
{
    self = [super initWithFrame:frame];
    
    if (self) {
                
    self.carouselArray = [NSArray arrayWithArray:array];
    [self createCarouselView];
}
    
    return self;
}

-(void)createCarouselView{
    
    self.carouselView = [[UIScrollView alloc]initWithFrame:self.frame];
    //self.carouselView.backgroundColor = [UIColor orangeColor];
    self.carouselView.bounces = NO;
    
    self.carouselView.pagingEnabled = YES;
    self.carouselView.showsHorizontalScrollIndicator = NO;
    self.carouselView.delegate = self;
    
    self.carouselView.contentSize = CGSizeMake(self.frame.size.width * self.carouselArray.count, 0);
    [self addSubview:self.carouselView];
     [_carouselView release];
    /*************** 设置pageControl ****************/
    self.page =[[UIPageControl alloc]initWithFrame:CGRectMake(self.frame.size.width-190, self.frame.size.height-30, 190, 31)];
    
    //当前选中点的颜色
    self.page.currentPageIndicatorTintColor = [UIColor grayColor];
    
    self.page.backgroundColor = [UIColor clearColor];
    
    self.page.pageIndicatorTintColor = [UIColor whiteColor];
    
    self.page.numberOfPages = self.carouselArray.count;
    
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    [self addSubview:self.page];
    
    [self bringSubviewToFront:_page];
    
    
    [_page release];
    
    for (int i = 0; i < self.carouselArray.count; i++) {
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.carouselView.frame.size.width * i, 0, self.carouselView.frame.size.width, self.carouselView.frame.size.height)];
        imageView.userInteractionEnabled = YES;
        imageView.tag = i+1;
        //        imageView.backgroundColor = [UIColor orangeColor];
        
        
        CarouselModel *carouselModel = self.carouselArray[i];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:carouselModel.image_url] placeholderImage:[UIImage imageNamed:@"u=1023456699,842778947&fm=21&gp=0"]];
        
        [self.carouselView addSubview:imageView];
        
        [imageView release];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        
        [imageView addGestureRecognizer:tap];
        [tap release];
        
        /*************** 添加label ****************/
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, imageView.frame.size.height - 30, imageView.frame.size.width, 31)];
        
        label.text = [NSString stringWithFormat:@" %@",carouselModel.food_name];
    
        label.textColor = [UIColor darkGrayColor];
        
        label.alpha = 0.8;
        
        label.backgroundColor = [UIColor colorWithRed:0.878 green:0.843 blue:0.820 alpha:1];
        
        [imageView addSubview:label];
        
        [label release];
        
        
    }
    
    
    [self carouselStart];
}

-(void)tap:(UITapGestureRecognizer *)tap
{
    UIImageView *picture = (UIImageView *)tap.view;
    
    [self.delegate getTag:picture.tag-1];
    
   // NSLog(@"%ld",picture.tag);
}
-(void)carouselStart
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    
}


-(void)changeImage
{
    
    if (self.isDragging) {
        
        return;
    }
    
    CGPoint pro = self.carouselView.contentOffset;
    
    
    pro.x += self.carouselView.frame.size.width;
    
    if (pro.x >= self.carouselArray.count * self.carouselView.frame.size.width) {
        
        pro.x = 0;
        
    }
    
    self.carouselView.contentOffset = CGPointMake(pro.x , 0);
    
    
}



#pragma mark 如果轮播图被拖拽, 拖拽状态设置为yes
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.isDragging = YES;
    NSLog(@"拖拽开始");
    
    if (scrollView.contentOffset.x == (self.carouselArray.count-1) * self.carouselView.frame.size.width)
    {
        self.carouselView.contentOffset = CGPointMake(0 , 0);
    }
    
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.isDragging = NO;
    NSLog(@"🍍拖拽停止");
}



//-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//     NSLog(@"拖拽停止");
//    
//}

#pragma mark 根据拖拽的偏移量变换分页控制器的页数显示
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    self.page.currentPage = (scrollView.contentOffset.x) / self.carouselView.frame.size.width;
    
    
}


-(void)pageAction:(UIPageControl *)pageControl
{
    self.carouselView.contentOffset = CGPointMake(self.carouselView.frame.size.width * pageControl.currentPage, 0);
    
}

@end
